<template>
  <el-form :model="yqm">
    <el-form-item label="生成邀请码"></el-form-item>
    <el-form-item label="设置可使用次数">
      <el-input v-model="yqm.num" type="number" style="max-width: 10rem" />
    </el-form-item>
  </el-form>
  <el-button @click="yaoqingma">生成</el-button>
  <el-divider />
  <el-table
    :data="InvitationList"
    border
    style="width: 100%; max-height: 50rem; overflow-y: auto"
  >
    <el-table-column prop="iid" label="邀请码ID" />
    <el-table-column prop="invitation" label="邀请码" />
    <el-table-column prop="nickname" label="使用者昵称" />
    <el-table-column prop="account" label="使用者邮箱" />
    <el-table-column prop="time" label="注册时间" />
  </el-table>
  <el-pagination
    :current-page="page"
    background
    :page-size="50"
    layout="prev, pager, next, total"
    :total="invitationNum"
    @current-change="refreshInvitationList"
    style="margin: 2rem 0"
  />
</template>
<script lang="ts">
import Method from '@/globalmethods.ts'
import { ElMessageBox } from 'element-plus'
import { watch } from 'vue'

export default {
  name: 'admin-invite',
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      isLoadingData: false,
      isLockAccountDialogVisible: false,
      isLocking: false,
      isInjecting: false,
      isPassing: false,
      yqm: {
        num: 1,
      },
      InvitationList: [],
      invitationNum: 0,
    }
  },
  methods: {
    yaoqingma() {
      // 设置请求参数
      const data = {
        avail_num: this.yqm.num,
      }

      // 发送POST请求
      Method.api_post('/user/generate_invitation', data)

        .then(async (res: any) => {
          const obj = res.data
          if (obj.code == 200) {
            ElMessageBox.alert(`邀请码如下：\n${obj.data}`, '生成邀请码', {
              confirmButtonText: '确定',
              callback: () => {
                Method.copyText(obj.data)
              },
            })
          }
        })
        .catch((err: any) => {
          console.log(err)
        })
    },
    refreshInvitationList() {
      Method.api_post('/admin/getInviteCode', {
        page: this.page,
        limit: 50,
      })
        .then((res: any) => {
          let obj = res.data
          if (obj.code == 200) {
            for (const el of obj.data) {
              el.time = Method.formatNormalTime(el.time)
            }
            this.InvitationList = obj.data
            this.invitationNum =
              this.invitationNum == 0 ? obj.sum : this.invitationNum
          }
        })
        .catch((error: any) => {
          console.log(error)
        })

      // Method.api_post('/user/my_invitation_code', {})
      //   .then((res: any) => {
      //     // 打印返回的JSON

      //     const obj = res.data
      //     console.log(obj)
      //   })
      //   .catch((err: any) => {
      //     console.log(err)
      //   })
    },
  },
  mounted() {
    this.refreshInvitationList()
    watch(
      () => this.page,
      () => {
        this.refreshInvitationList()
      },
    )
  },
}
</script>
